const path = require('path')
// 打包体积分析
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

function resolve(dir) {
	return path.join(__dirname, './', dir)
}

const autoprefixer = require('autoprefixer');
const pxtorem = require('postcss-pxtorem');

module.exports = {
	productionSourceMap: false, // 生产环境下打开sourceMap，辅助定位错误位置
	chainWebpack: config => {
		// 这里是对环境的配置，不同环境对应不同的BASE_URL，以便axios的请求地址不同
		config.plugin('define').tap(args => {
			args[0]['process.env'].BASE_URL = JSON.stringify(process.env.BASE_URL)
			console.log(args);
			return args
		})

		//  删除预加载
		config.plugins.delete('preload') // TODO: need test
		config.plugins.delete('prefetch') // TODO: need test

		// set svg-sprite-loader
		config.module
			.rule('svg')
			.exclude.add(resolve('src/icons'))
			.end()
		config.module
			.rule('icons')
			.test(/\.svg$/)
			.include.add(resolve('src/icons'))
			.end()
			.use('svg-sprite-loader')
			.loader('svg-sprite-loader')
			.options({
				symbolId: 'icon-[name]'
			})
			.end()

		// 添加别名
		config.resolve.alias
		.set('@', resolve('src'))
		.set('assets',resolve('src/assets'))
		.set('style',resolve('src/style'))
		.set('api',resolve('src/api'))
		.set('util',resolve('src/util'))
		.set('store',resolve('src/store'))
		.set('router',resolve('src/router'))
		.set('components',resolve('src/components'))
		
		// 打包分析
		if (process.env.NODE_ENV == 'analyz') {
			config.plugin('webpack-report').use(BundleAnalyzerPlugin, [{
				analyzerMode: 'static'
			}])
		}
	},
	css: {
		loaderOptions: {
			postcss: {
				plugins: [
					autoprefixer(),
					pxtorem({
						rootValue: 37.5,
						propList: ['*']
					})
				]
			}
		}
	},
	devServer: {
		proxy: {
			[process.env.BASE_URL]: {
				target: 'http://app.duiduishui.nfidea.com/', // target host
				secure: true, // 如果是https接口需要配置
				changeOrigin: true, // needed for virtual hosted sites
				pathRewrite: {
					['^' + process.env.BASE_URL]: '' // rewrite path
				}
			},
		}
	},
	publicPath: './'
}
